package com.borrowed.book.book.util;

import com.borrowed.book.book.constant.BookConstant;
import com.borrowed.book.book.domain.InitalBookPayOutDtoOut;
import com.borrowed.book.book.domain.InitialCardPayDtoOut;
import com.borrowed.book.membercard.entity.SMemberCard;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 订单通用方法
 */
public  class PayorderUtil {


    /**
     *会员卡有效期转换成文字说明
     */
    public static InitialCardPayDtoOut getComplexDate(SMemberCard carInfo){
        InitialCardPayDtoOut cardPay=new InitialCardPayDtoOut();
        BeanUtils.copyProperties(carInfo,cardPay);
        if(BookConstant.TIME_UNIT_WEEK.equals(carInfo.getValidTimeUnit())){
            cardPay.setValidTimeStr(carInfo.getValidTime()+"周");
        }else if(BookConstant.TIME_UNIT_MONTH.equals(carInfo.getValidTimeUnit())){
            cardPay.setValidTimeStr(carInfo.getValidTime()+"个月");
        }else if(BookConstant.TIME_UNIT_QUARTER.equals(carInfo.getValidTimeUnit())){
            cardPay.setValidTimeStr(3*carInfo.getValidTime()+"个月");
        }else if(BookConstant.TIME_UNIT_YEAR.equals(carInfo.getValidTimeUnit())){
            cardPay.setValidTimeStr(carInfo.getValidTime()+"年");
        }
        cardPay.setSettlePrice(carInfo.getCardPrice());
        return cardPay;
    }


    /***
     * 计算收发货日期
     * @param info
     */
    public static  void calculateDate(InitalBookPayOutDtoOut info){
        //预计发货时间
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1); //默认预计第二天发货
        info.setExpectDeliveryTime(calendar.getTime());
        //借阅开始时间
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+3); //默认第四天借阅开始
        info.setBorrowStartTime(calendar.getTime());
        //借阅结束时间
        calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);//默认借阅一个月
        info.setBorrowEndTime(calendar.getTime());
    }

    /**
     * 时间+随机四位数生成订单号
     * @return
     */
    public static String createOrderNo(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String shu= RandomStringUtils.randomAlphanumeric(4);
        String randId = sdf.format(new Date()) + shu;
        return randId;
    }
}
